package models

import "time"

type Complaint struct {
	ID int
	Category int
	Quote int
	UserId int
	Phone string
	Username string
	TargetId int
	Target string
	CreatedAt time.Time
	Item string
	Status int
}

func (c *Complaint) TableName() string {
	return "complaint"
}

type ArticleComplaint struct {
	ID int `json:"id"`
	Phone string `json:"phone"`
	Informant string `json:"informant"`
	Criminal string `json:"criminal"`
	CreatedAt time.Time `json:"created_at"`
	Status int `json:"status"`
	Item string `json:"item"`
	Operation string `json:"operation"`
	Replay string `json:"replay"`
}

func ArticleComplaintList(page int, pageSize int, item string, status string, criminal string, beginTime string, endTime string) ([]*ArticleComplaint,*Paginator){
	var res []*ArticleComplaint
	model := DB.Table("complaint").Select("id,phone,username as informant,target as criminal,created_at,status,item").Where("type=2")
	if item != ""{
		model = model.Where("type like ?","%"+item+"%")
	}
	if status != ""{
		model = model.Where("status=?", status)
	}
	if criminal != ""{
		model = model.Where("target like ?", "%"+criminal+"%")
	}
	if beginTime != "" {
		model = model.Where("created_at > ?",beginTime)
	}
	if endTime != "" {
		model = model.Where("created_at < ?",endTime)
	}
	var count int
	model.Count(&count)
	paginate := NewPage(page, pageSize, count)
	model.Offset(paginate.Offset).Limit(paginate.Limit).Order("created_at desc").Find(&res)
	return res, &paginate
}

type PageComplaint struct {
	ID int `json:"id"`
	Phone string `json:"phone"`
	Informant string `json:"informant"`
	Criminal string `json:"criminal"`
	CreatedAt time.Time `json:"created_at"`
	Status int `json:"status"`
	Item string `json:"item"`
}

func PageComplaintList(page int, pageSize int, item string, status string, criminal string, beginTime string, endTime string) ([]*PageComplaint,*Paginator){
	var res []*PageComplaint
	model := DB.Table("complaint").Select("id,phone,username as informant,target as criminal,created_at,status,item").Where("type=4")
	if item != ""{
		model = model.Where("type like ?","%"+item+"%")
	}
	if status != ""{
		model = model.Where("status=?", status)
	}
	if criminal != ""{
		model = model.Where("target like ?", "%"+criminal+"%")
	}
	if beginTime != "" {
		model = model.Where("created_at > ?", beginTime)
	}
	if endTime != "" {
		model = model.Where("created_at < ?", endTime)
	}
	var count int
	model.Count(&count)
	paginate := NewPage(page, pageSize, count)
	model.Offset(paginate.Offset).Limit(paginate.Limit).Find(&res)
	return res, &paginate
}


func ComplaintOne(id int, cate int) interface{} {
	var res struct{
		ID int `json:"id"`
		Phone string `json:"phone"`
		Username string `json:"name"`
		Target string `json:"target"`
		Quote string `json:"quote"`
		CreatedAt time.Time `json:"created_at"`
		Item string `json:"type"`
		Content string `json:"content"`
		Status int `json:"status"`
		Operation string `json:"operation"`
		Replay string `json:"replay"`
	}
	if cate == 2 {
		DB.Table("complaint as c").Select("c.id,c.phone,c.username,c.target,c.created_at,c.item,c.content,c.status,c.operation,.c.replay,a.title as quote").Where("c.id=?", id).Where("c.type=?",cate).Joins("left join articles a on a.id=c.quote").Scan(&res)
	}
	if cate == 4 {
		DB.Table("complaint as c").Select("c.id,c.phone,c.username,c.target,c.created_at,c.item,c.content,c.status,c.operation,.c.replay,a.title as quote").Where("c.id=?", id).Where("c.type=?",cate).
			Joins("left join comments as co on co.id=c.quote").
			Joins("left join articles a on a.id=co.a_id").Scan(&res)

	}
	return res
}

func GetComplaintById(id int) (*Complaint) {
	var res Complaint
	DB.Table("complaint").Where("id=?", id).First(&res)
	return &res
}